package bb.lanxing.lib.devices.remote;

import android.os.DeadObjectException;
import android.os.RemoteException;
import bb.lanxing.lib.devices.api.ConnectionListener;
import bb.lanxing.lib.devices.api.DeviceManager;
import bb.lanxing.lib.devices.api.SmartDevice;
import bb.lanxing.lib.devices.base.DeviceContext;
//import bb.lanxing.lib.devices.bici.IRemoteBiciController;
//import bb.lanxing.lib.devices.bici.IRemoteBiciControllerImpl;
import bb.lanxing.lib.devices.ble.cadence.IRemoteSwitchableCadenceController;
import bb.lanxing.lib.devices.ble.heartrate.IRemoteHeartRateBeltController;
import bb.lanxing.lib.devices.ble.heartrate.RemoteHeartRateBeltControllerImpl;
import bb.lanxing.lib.devices.ble.switchablecadence.RemoteSwitchableCadenceControllerImpl;
import bb.lanxing.lib.devices.ble.x1.IRemoteX1Contraoller;
import bb.lanxing.lib.devices.ble.x1.RemoteX1ControllerImpl;
//import bb.lanxing.lib.devices.sprint.ISprintController;
//import bb.lanxing.lib.devices.sprint.ISprintControllerImpl;


public class IRemoteDeviceServiceImpl extends IRemoteDeviceService.Stub implements ConnectionListener {
    private IRemoteConnectionStateListener mListener;

    @Override
    public IRemoteX1Contraoller getX1Controller(String str) throws RemoteException {
        if (DeviceContext.getDeviceManager() != null) {
            return new RemoteX1ControllerImpl(str);
        }
        return null;
    }

    @Override
    public IRemoteHeartRateBeltController getHeartRateBeltController(String str) throws RemoteException {
        if (DeviceContext.getDeviceManager() != null) {
            return new RemoteHeartRateBeltControllerImpl(str);
        }
        return null;
    }

    @Override
    public IRemoteSwitchableCadenceController getSwitchableCadenceController(String str) throws RemoteException {
        if (DeviceContext.getDeviceManager() != null) {
            return new RemoteSwitchableCadenceControllerImpl(str);
        }
        return null;
    }

    /*
    @Override
    public ISprintController getSprintController(String str) throws RemoteException {
        if (DeviceContext.getDeviceManager() != null) {
            return new ISprintControllerImpl(DeviceManagerHelper.getXossDeviceController(str));
        }
        return null;
    }*/

    @Override
    public void registerConnectionStateListener(IRemoteConnectionStateListener iRemoteConnectionStateListener) throws RemoteException {
        this.mListener = iRemoteConnectionStateListener;
        DeviceManager deviceManager = DeviceContext.getDeviceManager();
        if (deviceManager != null) {
            deviceManager.registerConnectionStateListener(this);
        }
    }

    @Override
    public void unregisterConnectionStateListener(IRemoteConnectionStateListener iRemoteConnectionStateListener) throws RemoteException {
        this.mListener = null;
        DeviceManager deviceManager = DeviceContext.getDeviceManager();
        if (deviceManager != null) {
            deviceManager.unregisterConnectionStateListener(this);
        }
    }

    @Override
    public void connect(byte[] bArr) throws RemoteException {
        DeviceManager deviceManager = DeviceContext.getDeviceManager();
        if (deviceManager != null) {
            deviceManager.connect(DeviceContext.createDeviceFromByteArray(bArr));
        }
    }

    @Override
    public void disconnect(int i) throws RemoteException {
        DeviceManager deviceManager = DeviceContext.getDeviceManager();
        if (deviceManager != null) {
            deviceManager.disconnect(i);
        }
    }

    @Override
    public boolean isConnected(int i) throws RemoteException {
        DeviceManager deviceManager = DeviceContext.getDeviceManager();
        return deviceManager != null && deviceManager.isConnected(i);
    }

    @Override
    public void disconnect2(String str) throws RemoteException {
        DeviceManager deviceManager = DeviceContext.getDeviceManager();
        if (deviceManager != null) {
            deviceManager.disconnect(str);
        }
    }

    @Override
    public boolean isConnected2(String str) throws RemoteException {
        DeviceManager deviceManager = DeviceContext.getDeviceManager();
        return deviceManager != null && deviceManager.isConnected(str);
    }

    @Override
    public boolean isConnecting(String str) throws RemoteException {
        DeviceManager deviceManager = DeviceContext.getDeviceManager();
        return deviceManager != null && deviceManager.isConnecting(str);
    }

    @Override
    public void release() throws RemoteException {
        DeviceManager deviceManager = DeviceContext.getDeviceManager();
        if (deviceManager != null) {
            deviceManager.release();
        }
    }

    /*
    @Override
    public IRemoteBiciController getBiciController(String str) throws RemoteException {
        if (DeviceContext.getDeviceManager() != null) {
            return new IRemoteBiciControllerImpl(DeviceManagerHelper.getBiciController());
        }
        return null;
    }*/

    @Override
    public void onConnectionStateChanged(SmartDevice smartDevice, int i, int i2) {
        if (this.mListener != null) {
            try {
                this.mListener.onStateChanged(DeviceContext.createByteArrayFromDevice(smartDevice), i, i2);
            } catch (RemoteException e) {
                e.printStackTrace();
                if (!(e instanceof DeadObjectException)) {
                    return;
                }
                this.mListener = null;
            }
        }
    }
}
